package com.lab.image.process;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;

import com.lab.utils.DisplayUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by ak
 */
public class MakeImageUtil {

    private static final int BLUR_IMAGE_SIZE = 100;

    public static Bitmap takeScreenShot(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Bitmap result = resizeBitmap(b1);
        view.destroyDrawingCache();
        BlurProcessor processor = new BlurProcessor();
        result = processor.blur(result, 8f);
        return result;
    }

    public static Bitmap takeScreenShot(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Bitmap result = resizeBitmap(b1);
        view.destroyDrawingCache();
        BlurProcessor processor = new BlurProcessor();
        result = processor.blur(result, 8f);
        return result;
    }

    private static Bitmap resizeBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int resizeWidth = width;
        int resizeHeight = height;
        //按大的边等比例缩小
        if (width > height) {
            if (width > BLUR_IMAGE_SIZE) {
                resizeWidth = BLUR_IMAGE_SIZE;
                resizeHeight = height * BLUR_IMAGE_SIZE / width;
            }
        } else {
            if (height > BLUR_IMAGE_SIZE) {
                resizeWidth = width * BLUR_IMAGE_SIZE / height;
                resizeHeight = BLUR_IMAGE_SIZE;
            }
        }
        return Bitmap.createScaledBitmap(bitmap,
                resizeWidth, resizeHeight, true);
    }



    public static void layoutView(View v, int width, int height) {
        v.layout(0, 0, width, height);
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
        v.measure(measuredWidth, measuredHeight);
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    }



    public static Bitmap convertViewToBitmap(View view){

        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();  //启用DrawingCache并创建位图
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }

    public static File saveBitmap(String path,Bitmap bitmap, String bitName) {
        File file = new File(path+ bitName);
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
                out.flush();
                out.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}